//
//  CheckHelper.m
//  CarNetwork_Shipper
//
//  Created by bubo on 15/10/29.
//  Copyright © 2015年 turingEdu. All rights reserved.
//

#import "CheckHelper.h"

@implementation CheckHelper

/**
 *  判断字符串是否为空
 *
 *  @param string 字符串
 *
 *  @return 返回YES为空 NO不为空
 */
+(BOOL)checkEmptyString:(NSString*)string {
    
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]] || [[string class] isSubclassOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    if([string isEqualToString:@""] || [string isEqualToString:@"<null>"] || [string isEqualToString:@"(null)"])
    {
        return YES;
    }
    return NO;
}

/**
 *  验证手机号正则表达式
 *
 *  @param phoneNum 手机号
 *
 *  @return NO：手机号格式不正确 YES：手机号正确
 */
+(BOOL)checkPhoneNumInput:(NSString*)phoneNum{
    
    NSString *regex = @"^((13[0-9])|(146)|(147)|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:phoneNum];
    
    return isMatch;
}

/**
 *  数字字符校验
 *
 *  @param string 字符串
 *
 *  @return 返回 YES NO
 */
+(BOOL)checkNumber:(NSString*)string{
    
    NSString *numbersperiod=@"0123456789.";
    NSCharacterSet*cs;
    cs =[[NSCharacterSet characterSetWithCharactersInString:numbersperiod]invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
    BOOL Result = [string isEqualToString:filtered];
    return Result;
}

/**
 *  判断用户名，在4－16位
 *
 *  @param text 字符串
 *
 *  @return 返回 YES NO
 */
+(BOOL)checkName:(NSString*)text{
    
    NSString *Regex = @"^[A-Za-z0-9]{4,16}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    BOOL isMatch = [pred evaluateWithObject:text];
    return isMatch;
}

/**
 *  判断昵称，只允许中文，英文，数字，字母，横杠，下划线
 *
 *  @param text 字符串
 *
 *  @return 返回 YES NO
 */
+(BOOL)checkNikeName:(NSString*)text{
    
    NSString *Regex = @"^(\\w|-|[\u4E00-\u9FA5])*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    BOOL isMatch = [pred evaluateWithObject:text];
    return isMatch;
}

/**
 *  判断手机号码，1开头的十一位数字
 *
 *  @param text 字符串
 *
 *  @return 返回 YES NO
 */
+(BOOL)checkMobile:(NSString*)text{
    
    NSString *Regex = @"1\\d{10}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    BOOL isMatch = [pred evaluateWithObject:text];
    return isMatch;
}

/**
 *  判断邮箱
 *
 *  @param text 字符串
 *
 *  @return 返回 YES NO
 */
+(BOOL)checkEmail:(NSString*)text{
    
    NSString *Regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    BOOL isMatch = [pred evaluateWithObject:text];
    return isMatch;
}

/**
 *  判断密码，6－16位
 *
 *  @param text 字符串
 *
 *  @return 返回 YES NO
 */
+(BOOL)checkPassWord:(NSString*)text{
    
    NSString *Regex = @"\\w{6,16}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    BOOL isMatch = [pred evaluateWithObject:text];
    return isMatch;
}

@end
